/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewPanel.java
 *
 * Created on 20.5.2011, 22:02:05
 */
package forms;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import src.CustomCellRenderer;
import src.Evidence;
import src.SouborIO;
import src.Zak;

/**
 *
 * @author Viktor
 */
public class ViewPanel extends javax.swing.JPanel implements TableModelListener, ListSelectionListener {

    JTable table;
    Evidence e;
    Object[][] draw;
    Object[] colhead;
    Zak zobrZak;
    InfoPanel ip;
    int soucmesic;
     SouborIO io;
     boolean deleting;
     boolean lock; //zamykání jen pro současný den
     int den;
     boolean freeze; //zamknutí tabulky, že nepůjde editovat docházka
     CustomCellRenderer renderer;

    /** Creates new form ViewPanel */
    public ViewPanel(Evidence e, Zak zobrZak, InfoPanel ip, int mesic, int den) {
        
        initComponents();
        setSize(1000, 600);
        soucmesic = mesic;
       deleting = false;
       lock = false;
       freeze = false;
       this.den = den;
table = null;
//        drawTable();
renderer = new CustomCellRenderer();
        this.e = e;
        this.zobrZak = zobrZak;
        this.ip = ip;
       io = new SouborIO();
        
   //    table.setDefaultRenderer(Integer.class, renderer); //vlastni renderer
        toDraw();

     //   this.add(table.getTableHeader(), BorderLayout.PAGE_START);
     //   this.add(table, BorderLayout.CENTER);
//        add(table);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables


    public void setEvidence(Evidence ej) {
        e = ej;
    }

    public void setDeleting(boolean deleting) {
        this.deleting = deleting;
    }

    
    private void toDraw() {

        int pocetdni = e.getPocetDni(soucmesic);
        //System.out.println("Pocet dni  :" + pocetdni); //jen na zkousku
        draw = new Object[30][pocetdni+1];
        colhead = new Object[pocetdni+1];
        
        int j = 0;
        
        int[] mesic;
        for (int i = 0; i < colhead.length; i++) {

            if (i == 0) {
                colhead[i] = "Jméno";
            } else {
                colhead[i] = i;
            }

        }

        for (Iterator it = e.getZaci().iterator(); it.hasNext();) {

            Zak z = (Zak) it.next();
            String s = z.getPrijmeni() + " " + z.getJmeno();
           // s.concat(" " + z.getJmeno());
            //pom = z.getPole();
            mesic = z.zpristupniMesic(soucmesic);

            draw[j][0] = s;
            for (int i = 0; i < pocetdni; i++) {        
                draw[j][i + 1] = mesic[i];        

            }

            j++;
        }

        table = new JTable(draw, colhead){

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            
          
            }

         
            
            
        };
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.getTableHeader().setReorderingAllowed(false);

        TableColumn column;
        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            if (i == 0) {
                column.setPreferredWidth(100); 
            } else {
              // column.setCellRenderer(renderer);
                column.setPreferredWidth(8);
            }
        }
    // table.setDefaultRenderer(Zak.class, renderer);
     
 
 
 
        table.getSelectionModel().addListSelectionListener(this);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setRowSelectionAllowed(true);
        
        table.getModel().addTableModelListener(this);
       
   //     table.getColumnModel().getSelectionModel().addListSelectionListener(this);
        this.add(table.getTableHeader(), BorderLayout.PAGE_START);
        this.add(table, BorderLayout.CENTER);
        //
        TableColumnModel cModel = table.getColumnModel();
        for(int i = 1; i < cModel.getColumnCount();i++){
            cModel.getColumn(i).setCellRenderer(renderer);
        }
        //**************************MOUSE LISTENER*****************************//
        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent evt) {
                //  super.mouseClicked(e);
                 Point p = evt.getPoint(); 
                  int rowindex = table.rowAtPoint(p);
                  int colindex = table.columnAtPoint(p);
                 
              
                  
                if (SwingUtilities.isLeftMouseButton(evt)) { // je stiskle leve tlacitko mysi
                    
                    try {

                        if (rowindex != -1) {
                            zobrZak = e.zpristupniZaka(rowindex);
                            ip.setZobrZak(zobrZak);
                            
                            if (colindex > 0) {
                                TableModel tm = table.getModel();
                           //     Object ob = tm.getValueAt(rowindex, colindex);
                                // int pom = Integer.parseInt(ob.toString());
                                tm.setValueAt(1, rowindex, colindex);
                                e.getZaci().get(rowindex).zpristupniMesic(soucmesic)[colindex - 1] = 1;
                                io.ulozit("evidence.bin", e);


                            }
                        }
                    } catch (java.lang.IndexOutOfBoundsException exx) {
                    }
                    
                }
                if (SwingUtilities.isRightMouseButton(evt)) { //je stiskle prave tlacitko mysi
                   
                   

                    try {

                        if (rowindex != -1) {
                            zobrZak = e.zpristupniZaka(rowindex);
                            ip.setZobrZak(zobrZak);
                        
                            if (colindex > 0) {
                                TableModel tm = table.getModel();
                           //     Object ob = tm.getValueAt(rowindex, colindex);
                                // int pom = Integer.parseInt(ob.toString());
                                tm.setValueAt(0, rowindex, colindex);
                                e.getZaci().get(rowindex).zpristupniMesic(soucmesic)[colindex - 1] = 0;
                                io.ulozit("evidence.bin", e);


                            }
                        }
                    } catch (java.lang.IndexOutOfBoundsException exx) {
                    }

                }
            }
        });
       // table.getColumnModel().getColumn(1).setCellRenderer(renderer);
    }
// public void reDraw(){
//    for(int i = 0; i < 1;i++ ){
//        jTable1.add((Object) e.zpristupniZaka(i));
//        
//    }
// }

    public void tableChanged(TableModelEvent ev) {
        // toDraw();
      //  throw new UnsupportedOperationException("Not supported yet.");

    }
// Listener na výběr v tabulce

    public void valueChanged(ListSelectionEvent ev) {
////      int pom = ev.getFirstIndex();
//////      ev.
////      zobrZak = e.zpristupniZaka(pom); 
////      ip.setZobrZak(zobrZak);
////        throw new UnsupportedOperationException("Not supported yet.");
//
////       if (ev.getSource() == table.getColumnModel().getSelectionModel()
////               && table.getColumnSelectionAllowed() ){
//        int colindex = table.getSelectedColumn();
//        int rowindex = table.getSelectedRow();
//
//        try {
//            if (rowindex != -1) {
//                zobrZak = e.zpristupniZaka(rowindex);
//                ip.setZobrZak(zobrZak);
//                /*
//                Co kdyby se testoval výběr uvnitř tabulky a když by sloupec byl větší než 0, pak by se provedla změna ?
//                 */
//                if (colindex > 0) {
//                    
//                    if(!freeze){
//                        
//                    
//                    if(lock){
//                        if(colindex== den){
//                            
//                        
//                              TableModel tm = table.getModel();
//                    Object ob = tm.getValueAt(rowindex, colindex);
//                    int pom = Integer.parseInt(ob.toString());
//                    
//                    if(!deleting){
//                        
//                    
//                    if (pom == 0) {
//                        tm.setValueAt(1, rowindex, colindex);
//                       e.getZaci().get(rowindex).zpristupniMesic(soucmesic)[colindex-1] = 1;
//                       io.ulozit("evidence.bin", e);
//                       
//                       
//                    }
//                    }else{
//                        if(pom == 1){
//                            
//                        
//                        tm.setValueAt(0, rowindex, colindex);
//                       e.getZaci().get(rowindex).zpristupniMesic(soucmesic)[colindex-1] = 0;
//                       io.ulozit("evidence.bin", e);
//                       }
//                    }
//                            
//                        
//                        
//                        
//                        
//                        }
//                        
//                        
//                        return;
//                    }else{
//                        
//                    
////          Zak pomz = e.zpristupniZaka(rowindex);
////          pomz.prisel2(6, colindex);
////          toDraw();
////          System.out.println("Baf");
////          table.setValueAt(1, rowindex, colindex);
//                    
//                    /*tady bude k zpristupneni zaka a dne v mesici, ve kterem se zapise dochazka*/
//                    TableModel tm = table.getModel();
//                    Object ob = tm.getValueAt(rowindex, colindex);
//                    int pom = Integer.parseInt(ob.toString());
//                    
//                    if(!deleting){
//                        
//                    
//                    if (pom == 0) {
//                        tm.setValueAt(1, rowindex, colindex);
//                       e.getZaci().get(rowindex).zpristupniMesic(soucmesic)[colindex-1] = 1;
//                       io.ulozit("evidence.bin", e);
//                       
//                    }
//                    }else{
//                        if(pom == 1){
//                            
//                        
//                        tm.setValueAt(0, rowindex, colindex);
//                       e.getZaci().get(rowindex).zpristupniMesic(soucmesic)[colindex-1] = 0;
//                       io.ulozit("evidence.bin", e);
//                       }
//                    }
//                            
////      if(pom == 1){
////        tm.setValueAt(0, rowindex, colindex);  
////      }
//
//}
//                }else{
//                        
//                    }
//                       
//                    }
//            }
//        } catch (java.lang.IndexOutOfBoundsException exx) {
//        }
//
////       }
    }

    
    public void update() {
       
        this.remove(table.getTableHeader());
        this.remove(table);
         table = null;
        toDraw();

    }

    public boolean isLock() {
        return lock;
    }

    public void setLock(boolean lock) {
        this.lock = lock;
    }

    public boolean isFreeze() {
        return freeze;
    }

    public void setFreeze(boolean freeze) {
        this.freeze = freeze;
    }
    
   
    
}
// zpristupneni mesice, - v mainframu udelat ref promennou, do ktere ukladat z mytimepanelu - pres konstruktor to supnout sem
